//
//  HSListVM.m
//  HolyShit
//
//  Created by Robin on 2017/3/24.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSListVM.h"

@interface HSListVM ()

@property (nonatomic, assign) NSUInteger pos;

@end

@implementation HSListVM


- (RACCommand *)refreshCommand
{
    if (!_refreshCommand) {
        @weakify(self);
        if (appType == HSAppTypeCaiPiao) {
            _refreshCommand = [RACCommand commandWithSignalBlock:^RACSignal *(id input) {
                @strongify(self);
                NSString *url = @"c.m.163.com/nc/article/list/T1356600029035/0-20.html";
                return [[MYHttpHelper shareHelper] rac_getWithUrl:url params:nil success:^(NSDictionary *dict) {
                    self.pos = 0;
                    NSArray<HSListModel *> *array = [NSArray modelArrayWithClass:[HSListModel class] json:dict[@"T1356600029035"]];
                    NSMutableArray *tmpArray = @[].mutableCopy;
                    for (NSUInteger i = 1; i < array.count; ++i) {
                        if (!array[i].articleType && !array[i].skipType) {
                            [tmpArray addObject:array[i]];
                        }
                    }
                    self.topics = tmpArray;
                } failed:^(NSError *error) {
                    
                }].racSignal;
            }];
        }
        else if(appType == HSAppTypeTiYu){
            _refreshCommand = [RACCommand commandWithSignalBlock:^RACSignal *(id input) {
                @strongify(self);
                NSString *url = [NSString stringWithFormat:@"c.m.163.com/dlist/article/dynamic?from=T1348649079062&passport=&devId=Bu/fsXspxq%2BGcAtXZ0I%2Bx6J6zht8excSZlniCowtzsuDcFCdyA4MFVV8sELO65mr&version=21.0&spever=false&net=wifi&ts=%lu&sign=YGicULk5BHSxvxw9y9/vYlBLJ4bWqsq31dCbzNULHKh48ErR02zJ6/KXOnxX046I&encryption=1&canal=appstore&offset=10&size=10&fn=2",time(0)];
                return [[MYHttpHelper shareHelper] rac_getWithUrl:url params:nil success:^(NSDictionary *dict) {
                    self.pos = 0;
                    NSArray<HSListModel *> *array = [NSArray modelArrayWithClass:[HSListModel class] json:dict[@"T1348649079062"]];
                    NSMutableArray *tmpArray = @[].mutableCopy;
                    for (NSUInteger i = 1; i < array.count; ++i) {
                        if (!array[i].articleType) {
                            [tmpArray addObject:array[i]];
                        }
                    }
                    self.topics = tmpArray;
                } failed:^(NSError *error) {
                    
                }].racSignal;
            }];
        }
        else{
            _refreshCommand = [RACCommand commandWithSignalBlock:^RACSignal *(id input) {
                @strongify(self);
                NSString *url = [NSString stringWithFormat:@"c.m.163.com/dlist/article/dynamic?from=T1348654151579&passport=&devId=Bu/fsYspxq%2BGcAtXZ0I%2Bx6J6zht8excSZlniCowtzsuDcFCdyA4MFVV8sELO65mr&version=21.0&spever=false&net=wifi&ts=1490621702&sign=Gt9Hxcp2KSnYYV7RcQhVwwC0dMCc%2BT8ssLYnjEtqT7V48ErR02zJ6/KXOnxX046I&encryption=1&canal=appstore&offset=10&size=10&fn=2",time(0)];
                return [[MYHttpHelper shareHelper] rac_getWithUrl:url params:nil success:^(NSDictionary *dict) {
                    self.pos = 0;
                    NSArray<HSListModel *> *array = [NSArray modelArrayWithClass:[HSListModel class] json:dict[@"T1348654151579"]];
                    NSMutableArray *tmpArray = @[].mutableCopy;
                    for (NSUInteger i = 1; i < array.count; ++i) {
                        if (!array[i].articleType) {
                            [tmpArray addObject:array[i]];
                        }
                    }
                    self.topics = tmpArray;
                } failed:^(NSError *error) {
                    
                }].racSignal;
            }];
        }
    }
    return _refreshCommand;
}

- (RACCommand *)loadmoreCommand
{
    if (!_loadmoreCommand) {
        @weakify(self);
        if(appType == HSAppTypeCaiPiao){
            _loadmoreCommand = [RACCommand commandWithSignalBlock:^RACSignal *(id input) {
                @strongify(self);
                NSString *url = [NSString stringWithFormat:@"c.m.163.com/nc/article/list/T1356600029035/%lu-%lu.html", (unsigned long)self.pos + 20, self.pos + 40];
                return [[MYHttpHelper shareHelper] rac_getWithUrl:url params:nil success:^(NSDictionary *dict) {
                    if ([MYHttpHelper reqSuccessOfDic:dict]) {
                        self.pos += 20;
                        NSArray<HSListModel *> *array = [NSArray modelArrayWithClass:[HSListModel class] json:dict[@"T1356600029035"]];
                        NSMutableArray *tmpArray = @[].mutableCopy;
                        for (NSUInteger i = 1; i < array.count; ++i) {
                            if (!array[i].articleType && !array[i].skipType) {
                                [tmpArray addObject:array[i]];
                            }
                        }
                        if (tmpArray.count > 0) {
                            [self.topics addObjectsFromArray:tmpArray];
                        }
                    }
                } failed:^(NSError *error) {
                    
                }].racSignal;
            }];
        }
        else if(appType == HSAppTypeTiYu){
            _loadmoreCommand = [RACCommand commandWithSignalBlock:^RACSignal *(id input) {
                @strongify(self);
                NSString *url = [NSString stringWithFormat:@"c.m.163.com/dlist/article/dynamic?from=T1348649079062&passport=&devId=Bu/fsXspxq%2BGcAtXZ0I%2Bx6J6zht8excSZlniCowtzsuDcFCdyA4MFVV8sELO65mr&version=21.0&spever=false&net=wifi&ts=%lu&sign=YGicULk5BHSxvxw9y9/vYlBLJ4bWqsq31dCbzNULHKh48ErR02zJ6/KXOnxX046I&encryption=1&canal=appstore&offset=10&size=10&fn=2",time(0)];
                return [[MYHttpHelper shareHelper] rac_getWithUrl:url params:nil success:^(NSDictionary *dict) {
                    if ([MYHttpHelper reqSuccessOfDic:dict]) {
                        self.pos += 10;
                        NSArray<HSListModel *> *array = [NSArray modelArrayWithClass:[HSListModel class] json:dict[@"T1348649079062"]];
                        NSMutableArray *tmpArray = @[].mutableCopy;
                        for (NSUInteger i = 1; i < array.count; ++i) {
                            if (!array[i].articleType && !array[i].skipType) {
                                [tmpArray addObject:array[i]];
                            }
                        }
                        if (tmpArray.count > 0) {
                            [self.topics addObjectsFromArray:tmpArray];
                        }
                    }
                } failed:^(NSError *error) {
                    
                }].racSignal;
            }];
        }
        else{
            _loadmoreCommand = [RACCommand commandWithSignalBlock:^RACSignal *(id input) {
                @strongify(self);
                NSString *url = [NSString stringWithFormat:@"c.m.163.com/dlist/article/dynamic?from=T1348654151579&passport=&devId=Bu/fsYspxq%2BGcAtXZ0I%2Bx6J6zht8excSZlniCowtzsuDcFCdyA4MFVV8sELO65mr&version=21.0&spever=false&net=wifi&ts=1490621702&sign=Gt9Hxcp2KSnYYV7RcQhVwwC0dMCc%2BT8ssLYnjEtqT7V48ErR02zJ6/KXOnxX046I&encryption=1&canal=appstore&offset=10&size=10&fn=2",time(0)];
                return [[MYHttpHelper shareHelper] rac_getWithUrl:url params:nil success:^(NSDictionary *dict) {
                    if ([MYHttpHelper reqSuccessOfDic:dict]) {
                        self.pos += 10;
                        NSArray<HSListModel *> *array = [NSArray modelArrayWithClass:[HSListModel class] json:dict[@"T1348654151579"]];
                        NSMutableArray *tmpArray = @[].mutableCopy;
                        for (NSUInteger i = 1; i < array.count; ++i) {
                            if (!array[i].articleType && !array[i].skipType) {
                                [tmpArray addObject:array[i]];
                            }
                        }
                        if (tmpArray.count > 0) {
                            [self.topics addObjectsFromArray:tmpArray];
                        }
                    }
                } failed:^(NSError *error) {
                    
                }].racSignal;
            }];
        }
    }
    return _loadmoreCommand;
}

@end
